{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "1a2722f9",
   "metadata": {},
   "source": [
    "# 调用人脸对比函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 92,
   "id": "677adb5a",
   "metadata": {},
   "outputs": [],
   "source": [
    "import compare"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 93,
   "id": "e7c68921",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on module compare:\n",
      "\n",
      "NAME\n",
      "    compare\n",
      "\n",
      "FUNCTIONS\n",
      "    face_compare(API_key, API_sercret, image_path)\n",
      "        该函数为调用face++ face_detect接口\n",
      "\n",
      "FILE\n",
      "    e:\\api-artificial-intelligence\\week04\\compare.py\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(compare)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 94,
   "id": "809bbb27",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "API_Key = \"AXr0X-fVLzYtJg-Yo13sIqOnA08EUcA1\"\n",
    "API_Secret = \"FgzgKXqhaxO6rcQX7m2kCqkdjBdvBQLg\"\n",
    "\n",
    "# 3.准备base URL(face detect)\n",
    "base_url = \"https://api-cn.faceplusplus.com/facepp/v3/detect\"\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "053730c2",
   "metadata": {},
   "source": [
    "## 准备人脸数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 95,
   "id": "061130c0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1648625748,795fc912-a20f-456f-b04b-3e5522b5ed0c',\n",
       " 'time_used': 97,\n",
       " 'faces': [{'face_token': 'd29d6d11fcfa6e87118b7863fe942532',\n",
       "   'face_rectangle': {'top': 180, 'left': 102, 'width': 171, 'height': 171}}],\n",
       " 'image_id': 'L6pns8Z7qHuOIyRC7xmKhQ==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 95,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "compare.face_compare(API_Key,API_Secret,'yifei.jpg')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 96,
   "id": "23323768",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1648625749,fd4d81e3-29e5-4bb6-a3ba-93e5e6fe8e7b',\n",
       " 'time_used': 148,\n",
       " 'faces': [{'face_token': '09242f5b2e3ba6e54ff1628db24fbf13',\n",
       "   'face_rectangle': {'top': 276, 'left': 367, 'width': 213, 'height': 213}}],\n",
       " 'image_id': 'qkm4BVwENuB3jLzfnSdnSA==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 96,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "compare.face_compare(API_Key,API_Secret,'yifei_02.jpg')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 97,
   "id": "12614769",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'0196b82e296b5b6535bc6ee9821747fa'"
      ]
     },
     "execution_count": 97,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "face_token_01 = compare.face_compare(API_Key,API_Secret,'yifei.jpg')['faces'][0]['face_token']\n",
    "face_token_01"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 98,
   "id": "dd25e4f5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'0cb30cfcb0b53bab4fcfa98517193338'"
      ]
     },
     "execution_count": 98,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "face_token_02 = compare.face_compare(API_Key,API_Secret,'yifei_02.jpg')['faces'][0]['face_token']\n",
    "face_token_02"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 99,
   "id": "8c5666c4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1648625755,e4d8c165-d8ec-4f9f-ab3b-3f68f4ee5c40',\n",
       " 'time_used': 362,\n",
       " 'confidence': 91.177,\n",
       " 'thresholds': {'1e-3': 62.327, '1e-4': 69.101, '1e-5': 73.975}}"
      ]
     },
     "execution_count": 99,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "compare_url = 'https://api-cn.faceplusplus.com/facepp/v3/compare'\n",
    "payload = {\n",
    "    \"api_key\":API_Key,\n",
    "    \"api_secret\":API_Secret,\n",
    "    'face_token1':face_token_01,\n",
    "    'face_token2':face_token_02\n",
    "}\n",
    "r = requests.post(compare_url,params = payload)\n",
    "\n",
    "r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3daa8c23",
   "metadata": {},
   "source": [
    "# 调用人类分析函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 100,
   "id": "a06cf62e",
   "metadata": {},
   "outputs": [],
   "source": [
    "import analyze"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 101,
   "id": "11c758e4",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "API_Key = \"AXr0X-fVLzYtJg-Yo13sIqOnA08EUcA1\"\n",
    "API_Secret = \"FgzgKXqhaxO6rcQX7m2kCqkdjBdvBQLg\"\n",
    "\n",
    "# 3.准备base URL(face detect)\n",
    "base_url = \"https://api-cn.faceplusplus.com/facepp/v3/detect\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 102,
   "id": "23ea69f6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1648625761,2b26af8a-91d3-4e4f-8e5f-db9da62dba34',\n",
       " 'time_used': 79,\n",
       " 'faces': [{'face_token': '4c8eccb0faa9427f7290a563df398f37',\n",
       "   'face_rectangle': {'top': 180, 'left': 102, 'width': 171, 'height': 171}}],\n",
       " 'image_id': 'L6pns8Z7qHuOIyRC7xmKhQ==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 102,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "analyze.face_analyze(API_Key,API_Secret,'yifei.jpg')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 103,
   "id": "5be37f42",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'84ce2d0128790476deb1209d758b5bcb'"
      ]
     },
     "execution_count": 103,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "face_token_01 = analyze.face_analyze(API_Key,API_Secret,'yifei.jpg')['faces'][0]['face_token']\n",
    "face_token_01"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 104,
   "id": "2de8de6b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'time_used': 167,\n",
       " 'request_id': '1648625765,d2ea9b60-e1cf-4312-a2ed-2d9240fee7f5',\n",
       " 'faces': [{'attributes': {'gender': {'value': 'Female'},\n",
       "    'age': {'value': 21},\n",
       "    'blur': {'blurness': {'threshold': 50.0, 'value': 0.242},\n",
       "     'motionblur': {'threshold': 50.0, 'value': 0.242},\n",
       "     'gaussianblur': {'threshold': 50.0, 'value': 0.242}},\n",
       "    'smile': {'threshold': 50.0, 'value': 0.116}},\n",
       "   'face_rectangle': {'width': 171, 'top': 180, 'left': 102, 'height': 171},\n",
       "   'face_token': '84ce2d0128790476deb1209d758b5bcb'}]}"
      ]
     },
     "execution_count": 104,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "analyze_url = 'https://api-cn.faceplusplus.com/facepp/v3/face/analyze'\n",
    "payload = {\n",
    "    \"api_key\":API_Key,\n",
    "    \"api_secret\":API_Secret,\n",
    "    'face_tokens':face_token_01,\n",
    "    'return_attributes':'gender,age,smiling,blur'\n",
    "    \n",
    "}\n",
    "r = requests.post(analyze_url,params = payload)\n",
    "\n",
    "r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6346d822",
   "metadata": {},
   "source": [
    "# 调用人脸搜索模块"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "55e80fa7",
   "metadata": {},
   "source": [
    "### 先建立faceset"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 105,
   "id": "1d034035",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "import search\n",
    "\n",
    "API_Key = \"AXr0X-fVLzYtJg-Yo13sIqOnA08EUcA1\"\n",
    "API_Secret = \"FgzgKXqhaxO6rcQX7m2kCqkdjBdvBQLg\"\n",
    "\n",
    "# 3.准备base URL(face detect)\n",
    "base_url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/create\"\n",
    "\n",
    "r = requests.post(base_url,payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 106,
   "id": "a12d34a1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': '93541f1af2919e1a17c937e718fda70f',\n",
       " 'time_used': 848,\n",
       " 'face_count': 1,\n",
       " 'face_added': 1,\n",
       " 'request_id': '1648625772,a814ce55-fa87-4d30-9b9d-11c6afa33bd6',\n",
       " 'outer_id': '',\n",
       " 'failure_detail': []}"
      ]
     },
     "execution_count": 106,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 107,
   "id": "a62c09e9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'93541f1af2919e1a17c937e718fda70f'"
      ]
     },
     "execution_count": 107,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "faceset_token_01 = r.json()['faceset_token']\n",
    "faceset_token_01"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 108,
   "id": "d8559b39",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "import search\n",
    "\n",
    "API_Key = \"AXr0X-fVLzYtJg-Yo13sIqOnA08EUcA1\"\n",
    "API_Secret = \"FgzgKXqhaxO6rcQX7m2kCqkdjBdvBQLg\"\n",
    "\n",
    "# 3.准备base URL(face detect)\n",
    "base_url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/create\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 109,
   "id": "deefe785",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1648625776,a5c01833-74b4-4059-b0b7-0859d74058f2',\n",
       " 'time_used': 44,\n",
       " 'faces': [{'face_token': '9984cfb51368748345339e53f36d2ac5',\n",
       "   'face_rectangle': {'top': 180, 'left': 102, 'width': 171, 'height': 171}}],\n",
       " 'image_id': 'L6pns8Z7qHuOIyRC7xmKhQ==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 109,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "search.face_search(API_Key,API_Secret,'yifei.jpg')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 110,
   "id": "77731a5e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'b27519d966e95c7040304257380e7663'"
      ]
     },
     "execution_count": 110,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "face_token_01 = search.face_search(API_Key,API_Secret,'yifei.jpg')['faces'][0]['face_token']\n",
    "face_token_01"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 111,
   "id": "526ccb2b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1648625783,18d33cf6-e2c8-42b5-9138-c924c5c49c4d',\n",
       " 'time_used': 333,\n",
       " 'thresholds': {'1e-3': 62.327, '1e-5': 73.975, '1e-4': 69.101},\n",
       " 'results': [{'confidence': 97.161,\n",
       "   'user_id': '',\n",
       "   'face_token': '84ce2d0128790476deb1209d758b5bcb'}]}"
      ]
     },
     "execution_count": 111,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "search_url = 'https://api-cn.faceplusplus.com/facepp/v3/search'\n",
    "payload = {\n",
    "    \"api_key\":API_Key,\n",
    "    \"api_secret\":API_Secret,\n",
    "    'face_token':face_token_01,\n",
    "    'faceset_token':faceset_token_01\n",
    "    \n",
    "}\n",
    "r = requests.post(search_url,params = payload)\n",
    "\n",
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e7fa1c63",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
